home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Main.bin / IdentityScope.java < prev    next >
Text File  |  1998-09-22  |  7KB  |  217 lines

  1. /*
  2.  * @(#)IdentityScope.java    1.34 98/07/01
  3.  *
  4.  * Copyright 1995-1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  * 
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. package java.security;
  16.  
  17. import java.io.Serializable;
  18. import java.util.Enumeration;
  19. import java.util.Properties;
  20.  
  21. /** 
  22.  * <p>This class represents a scope for identities. It is an Identity 
  23.  * itself, and therefore has a name and can have a scope. It can also 
  24.  * optionally have a public key and associated certificates.
  25.  *
  26.  * <p>An IdentityScope can contain Identity objects of all kinds, including
  27.  * Signers. All types of Identity objects can be retrieved, added, and 
  28.  * removed using the same methods. Note that it is possible, and in fact
  29.  * expected, that different types of identity scopes will
  30.  * apply different policies for their various operations on the
  31.  * various types of Identities.
  32.  *
  33.  * <p>There is a one-to-one mapping between keys and identities, and 
  34.  * there can only be one copy of one key per scope. For example, suppose
  35.  * <b>Acme Software, Inc</b> is a software publisher known to a user.
  36.  * Suppose it is an Identity, that is, it has a public key, and a set of
  37.  * associated certificates. It is named in the scope using the name 
  38.  * "Acme Software". No other named Identity in the scope has the same 
  39.  * public  key. Of course, none has the same name as well.
  40.  *
  41.  * @see Identity
  42.  * @see Signer
  43.  * @see Principal
  44.  * @see Key
  45.  *
  46.  * @version     1.31, 01/27/97
  47.  * @author Benjamin Renaud */
  48. public abstract 
  49. class IdentityScope extends Identity {
  50.  
  51.     /* The system's scope */
  52.     private static IdentityScope scope;
  53.  
  54.     // initialize the system scope
  55.     private static void initializeSystemScope() {
  56.  
  57.     String classname = Security.getProperty("system.scope");
  58.  
  59.     if (classname == null) {
  60.         return;
  61.  
  62.         } else {
  63.  
  64.         try {
  65.         Class.forName(classname);
  66.         } catch (ClassNotFoundException e) {
  67.         Security.error("unable to establish a system scope from " +
  68.                    classname);
  69.         e.printStackTrace();
  70.         }
  71.     }
  72.     }
  73.  
  74.     /**
  75.      * This constructor is used for serialization only and should not
  76.      * be used by subclasses.
  77.      */
  78.     protected IdentityScope() {
  79.     this("restoring...");
  80.     }
  81.  
  82.     /**
  83.      * Constructs a new identity scope with the specified name.
  84.      *
  85.      * @param name the scope name.
  86.      */
  87.     public IdentityScope(String name) {
  88.     super(name);
  89.     }
  90.  
  91.     /**
  92.      * Constructs a new identity scope with the specified name and scope.
  93.      * 
  94.      * @param name the scope name.
  95.      * @param scope the scope for the new identity scope.
  96.      * 
  97.      * @exception KeyManagementException if there is already an identity 
  98.      * with the same name in the scope.
  99.      */
  100.     public IdentityScope(String name, IdentityScope scope) 
  101.     throws KeyManagementException {
  102.     super(name, scope);
  103.     }
  104.  
  105.     /**
  106.      * Returns the system's identity scope. See the
  107.      * "System Identity Scope" section in the <a href=
  108.      * "../guide/security/CryptoSpec.html#SysIdScope">
  109.      * Java Cryptography Architecture API Specification & Reference </a>.
  110.      * 
  111.      * @return the system's identity scope.
  112.      */
  113.     public static IdentityScope getSystemScope() {
  114.     if (scope == null) {
  115.         initializeSystemScope();
  116.     }
  117.     return scope;
  118.     }
  119.  
  120.  
  121.     /**
  122.      * Sets the system's identity scope. See the
  123.      * "System Identity Scope" section in the <a href=
  124.      * "../guide/security/CryptoSpec.html#SysIdScope">
  125.      * Java Cryptography Architecture API Specification & Reference </a>.
  126.      *
  127.      * @param scope the scope to set.
  128.      */
  129.     protected static void setSystemScope(IdentityScope scope) {
  130.     staticCheck("set.system.scope");
  131.     IdentityScope.scope = scope;
  132.     }
  133.  
  134.     /**
  135.      * Returns the number of identities within this identity scope.
  136.      * 
  137.      * @return the number of identities within this identity scope.
  138.      */
  139.     public abstract int size();
  140.  
  141.     /**
  142.      * Returns the identity in this scope with the specified name (if any).
  143.      * 
  144.      * @param name the name of the identity to be retrieved.
  145.      * 
  146.      * @return the identity named <code>name</code>, or null if there are
  147.      * no identities named <code>name</code> in this scope.
  148.      */
  149.     public abstract Identity getIdentity(String name);
  150.  
  151.     /**
  152.      * Retrieves the identity whose name is the same as that of the 
  153.      * specified principal. (Note: Identity implements Principal.)
  154.      *
  155.      * @param principal the principal corresponding to the identity
  156.      * to be retrieved.
  157.      * 
  158.      * @return the identity whose name is the same as that of the 
  159.      * principal, or null if there are no identities of the same name 
  160.      * in this scope.
  161.      */
  162.     public Identity getIdentity(Principal principal) {
  163.     return getIdentity(principal.getName());
  164.     }
  165.  
  166.     /**
  167.      * Retrieves the identity with the specified public key.
  168.      *
  169.      * @param key the public key for the identity to be returned.
  170.      *
  171.      * @return the identity with the given key, or null if there are
  172.      * no identities in this scope with that key.
  173.      */
  174.     public abstract Identity getIdentity(PublicKey key);
  175.  
  176.     /**
  177.      * Adds an identity to this identity scope.
  178.      *
  179.      * @param identity the identity to be added.
  180.      *
  181.      * @exception KeyManagementException if the identity is not
  182.      * valid, a name conflict occurs, another identity has the same
  183.      * public key as the identity being added, or another exception
  184.      * occurs. */
  185.     public abstract void addIdentity(Identity identity) 
  186.     throws KeyManagementException;
  187.  
  188.     /**
  189.      * Removes an identity from this identity scope.
  190.      *
  191.      * @param identity the identity to be removed.
  192.      *
  193.      * @exception KeyManagementException if the identity is missing,
  194.      * or another exception occurs.
  195.      */
  196.     public abstract void removeIdentity(Identity identity) 
  197.     throws KeyManagementException;
  198.  
  199.     /**
  200.      * Returns an enumeration of all identities in this identity scope.
  201.      * 
  202.      * @return an enumeration of all identities in this identity scope.
  203.      */
  204.     public abstract Enumeration identities();
  205.  
  206.     /**
  207.      * Returns a string representation of this identity scope, including
  208.      * its name, its scope name, and the number of identities in this
  209.      * identity scope.
  210.      *
  211.      * @return a string representation of this identity scope.
  212.      */
  213.     public String toString() {
  214.     return super.toString() + "[" + size() + "]";
  215.     }
  216. }
  217.